#!/usr/bin/env python
import os, os.path as path
Import('build')

Env = build.env.Clone()
sources = ['boot.cpp']
Env.Append(CPPDEFINES=['USE_NO_EXPORT=1', 'NO_UNZIP=1'])
Env.Append(CPPPATH=[path.join(build.kroll_source_dir, 'api')])

# Add the utils
Env.Append(CPPPATH=[build.kroll_utils_dir])
sources += build.get_kroll_utils(path.join(build.dir, 'objs', 'boot'), unzip=False)

if build.is_osx():
	sources += Glob('*_osx.mm')
	Env.Append(LIBS=['dl'])
	Env.Append(FRAMEWORKS=['Cocoa','IOKit'])

if build.is_linux():
	sources += Glob('*_linux.cpp')
	Env.Append(LIBS=['dl'])
	Env.ParseConfig('pkg-config --cflags --libs gtk+-2.0 gdk-2.0')

if build.is_win32():
	sources += Glob('*_win32.cpp') + [Env.RES('winboot.rc')]
	
	Env.Append(LIBS=['kernel32','shell32','user32','advapi32','iphlpapi','gdi32'])
	Env.Append(LINKFLAGS=['/NODEFAULTLIB:MSJAVA.lib'])
	ccf = Env['CCFLAGS']
	if '/Z7' in ccf: ccf.remove('/Z7')
	ccf += ['/O2','/GL','/FD','/EHsc','/W3','/Wp64','/Zi','/TP','/DUNICODE']
	Env['CCFLAGS'] = ccf

	# Generate a manifest which will later be merged with the common controls manifest
	intermediate_manifest = path.join(build.dir, 'boot.exe.intermediate.manifest')
	linkflags = ['/MANIFEST', '/MANIFESTFILE:%s' % intermediate_manifest]

	linkflags += ['/OPT:REF','/OPT:ICF','/LTCG','/MACHINE:X86']
	if ARGUMENTS.get('debug', 0) or ARGUMENTS.get('console',0):
		linkflags.append('/SUBSYSTEM:CONSOLE'),
		Env.Append(CPPDEFINES=['WIN32_CONSOLE=1', 'UNICODE=1'])
	Env.Append(LINKFLAGS=linkflags)

# Breakpad integration
breakpad = ARGUMENTS.get('breakpad', '1') != '0'
if not(build.is_32()) and build.is_linux():
	breakpad = False

if breakpad:
	Env.Append(CPPPATH = ['breakpad'])
	Env.Append(CPPDEFINES = [['USE_BREAKPAD', 1]])
	if 'CPPFLAGS' in Env and '-Werror' in Env['CPPFLAGS']:
		Env['CPPFLAGS'].remove('-Werror')

if breakpad and build.is_win32():
	sources += \
		Glob('breakpad/client/windows/crash_generation/*.cc') + \
		['breakpad/client/windows/handler/exception_handler.cc',
		 'breakpad/common/windows/guid_string.cc',
		 'breakpad/common/windows/http_upload.cc']
	Env.Append(LIBS=['wininet'])

elif breakpad:
	sources += \
		Glob('breakpad/common/*.c') + \
		Glob('breakpad/common/*.cc') + \
		Glob('breakpad/client/*.cc') + \
		Glob('breakpad/processor/*.cc')
		 
if breakpad and build.is_linux():
	sources += \
		Glob('breakpad/client/linux/handler/*.cc') + \
		Glob('breakpad/common/linux/*.cc')

if breakpad and build.is_osx():
	Env.Append(LIBS=['libcrypto'])
	sources += \
		Glob('breakpad/common/mac/dwarf/*.cc') + \
		Glob('breakpad/common/mac/*.cc') + \
		Glob('breakpad/common/mac/*.mm') + \
		Glob('breakpad/common/mac/*.m') + \
		Glob('breakpad/client/mac/handler/*.cc')

kboot_out = path.join(build.dir, 'runtime', 'template', 'kboot')
p = Env.Program(kboot_out, sources)
build.mark_build_target(p)

# Merge kboot's manifest with the common controls manifest, so that
# it can use XP-style common controls -- and still have the correct
# references to MSVCRT8. Also include Webkit.tlb in the manifest and
# have it reference the path "WebKit.dll". This is for XP systems which
# will need to bootstrap by running runtimedir/kboot.exe during the 
# boot process.
if build.is_win32():
	t = build.utils.CopyToDir(p[0], path.join(build.dir, 'runtime'))
	build.mark_build_target(t)

	webkit_tp = path.join(build.third_party, 'webkit')
	webkit_lib = path.join(webkit_tp, 'lib')
	webkit_tlb = path.join(webkit_lib, 'WebKit.tlb')

	cc_manifest = path.join(build.kroll_support_dir, 'common_controls.manifest')
	merged_manifest = path.join(build.dir, 'kboot.merged.manifest')
	Env.AddPostAction(p, 'mt.exe -nologo -manifest "%s" "%s" -out:"%s"' % (intermediate_manifest, cc_manifest, merged_manifest))
#	Env.AddPostAction(p, ('mt.exe -nologo /tlb:"%s" /dll:"WebKit" /outputresource:"%s.exe;#1" /manifest "%s"' % (webkit_tlb, kboot_out, merged_manifest)))
	Env.AddPostAction(p, ('mt.exe -nologo /outputresource:"%s.exe;#1" /manifest "%s"' % (kboot_out, merged_manifest)))

#	Env.AddPostAction(p, 'mt.exe -nologo -manifest "%s" -outputresource:"%s;1"' % (merged_manifest, p[0]))
